/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.arg.deploy.webapp.common;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import org.zkoss.zk.ui.Component;
import org.zkoss.zul.Cell;
import org.zkoss.zul.Label;
import org.zkoss.zul.Row;
import org.zkoss.zul.RowRenderer;

/**
 *
 * @author sorasaks
 */
public abstract class DeployRowRenderer implements RowRenderer {

    public final static String LEFT_CELL_STYLE = "left";
    public final static String CENTER_CELL_STYLE = "center";
    public final static String RIGHT_CELL_STYLE = "right";
    public final static String LABEL_STYLE = "content-cell";
    protected List<Row> row = new ArrayList<Row>();

    @Override
    public void render(Row row, Object data, int index) throws Exception {
        if (data instanceof String) {
            noRecordRender(row, data);
        } else {
            gridRender(row, data, index);
        }
    }

    private void noRecordRender(Row row, Object o) {
        Cell cell = new Cell();
        cell.setSclass(LEFT_CELL_STYLE);
        cell.setColspan(row.getGrid().getColumns().getChildren().size());
        Label noRecordLbl = new Label();
        noRecordLbl.setSclass(LABEL_STYLE);
        noRecordLbl.setValue((String) o);

        cell.appendChild(noRecordLbl);

        row.appendChild(cell);
    }
    
    protected void addListRow(Row row) {
        this.row.add(row);
    }

    protected void appendCellInRow(Row row, Cell cell) {
        row.appendChild(cell);
    }

    protected Cell createLabelInCell(String data, String cellStyle) {
        Cell cell = new Cell();
        cell.setAlign(cellStyle);
        Label label = new Label();
        label.setSclass(LABEL_STYLE);
        label.setValue(data);
        cell.appendChild(label);
        return cell;
    }

    public List<Row> getRowRenderer() {
        return row;
    }

    public HashMap<Row, List<Component>> getRowComponent() {
        HashMap<Row, List<Component>> param = new HashMap<Row, List<Component>>();
        for (Row r : row) {
            List<Component> comp = r.getChildren();
            List<Component> newComp = new ArrayList<Component>();
            for (Component c : comp) {
                Cell cell = (Cell) c;
                newComp.add(cell.getFirstChild());
            }
            param.put(r, newComp);
        }
        return param;
    }

    protected abstract void gridRender(Row row, Object o, int i) throws Exception;
}
